/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import com.trentech.billcalc.Biller;
import com.trentech.billcalc.FinanceManager;
import com.trentech.billcalc.Resident;

/**
 * OptOutEvent is an Event for when a resident wants to stop paying a bill
 * that does not apply to him.
 */
public class OptOutEvent extends Event {
    
    /**
     * The name of the Biller to opt out of.
     */
    private String billerName;
    
    
    /**
     * Constructor for OptOutEvent.
     * 
     * @param   date    the date that the resident stopped paying.  Cannot 
     *                  be null.
     * @param   name    the name of the resident.  Cannot be null or empty.
     * @param   biller  the name of the Biller to opt in for.  Cannot be 
     *                  null or empty.
     */
    public OptOutEvent(Date date, String name, String biller) {
        super(date, name);
        
        this.billerName = biller;
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        OptOutEvent otherCasted = (OptOutEvent) other;
        value = billerName.compareTo(otherCasted.getBillerName());
        if (value != 0) {
            return value;
        }

        return 0;
    }
    
    
    /**
     * Starts an opt-out period for the chosen Resident.
     * 
     * @param   manager the FinancialManager to help process this Event.  
     *                  Cannot be null.
     */
    public void process(FinanceManager manager) {
        Biller biller = manager.findBiller(billerName);
        Resident found = manager.findResident(getName());

        if (found != null) {
            found.optOutStart(biller, getDate());
        }
    }

    
    /**
     * Returns the biller name for the resident to opt out of.
     * 
     * @return  the name of the biller to stop paying.  Will not be null or 
     *          empty.
     */
    public String getBillerName() {
        return billerName;
    }
    
    
    /**
     * Sets the biller name for the resident to opt out of.
     * 
     * @param   billerName  the name of the biller to stop paying.  Cannot be
     *                      null or empty.
     */
    public void setBillerName(String billerName) {
        String oldBillerName = this.billerName;
        this.billerName = billerName;
        getSupport().firePropertyChange("billerName", oldBillerName, billerName);
        setSaved(false);
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
    
        super.validate(propertyName, manager);
        if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
        if (propertyName.equals("billerName")) {
            notNullOrEmptyString(propertyName, getBillerName());
            biller(propertyName, getBillerName(), manager);
        }
    }
}    
